package jia.core;

import jia.core.event.GameEvent;
import jia.core.graphics.FlatImageManager;
import jia.core.flags.SpaceFlags;

import java.awt.image.BufferedImage;
import java.util.HashSet;
import java.util.Set;

/**
 * Defines a square that the player, creature, or other object occupies for a given turn.
 * @author Erik Stell, Alec Lanter
 * @version 1.1
 * @see Wall
 * @see net.sf.jia.framework.event.GameEvent
 * @see SpaceFlags
 * @since 7/29/2005
 */
public class Space{
	protected long id;
	private String backdropImageName;
	private BufferedImage backdropImage;
	private Set<Wall> walls;
	private SpaceFlags flags;
	private GameEvent event;

	/**
	 * Default constructor for a Space object that contains a backdrop image, up to four wall objects,
	 * any specific flags that need to trigger on the space, as well as any events that also need to trigger.
	 */
	public Space(){
		backdropImage = null;
		walls = new HashSet<Wall>();
		flags = new SpaceFlags();
		event = null;
	}

	/**
	 * Adds a wall object to the given Space.
	 * @param wall
	 */
	public void addWall(Wall wall){
		if(wall.getParentSpace() != null) {
			throw new IllegalStateException("Wall already has a parent space.  A wall cannot belong to two spaces.");
		}
		walls.add(wall);
		wall.setParentSpace(this);
	}

	/**
	 * Removes a wall object from the given Space
	 * @param wall
	 */
	public void removeWall(Wall wall){
		walls.remove(wall);
		wall.setParentSpace(null);
	}

	/**
	 * Returns a Wall object at the direction provided.
	 * @param facing
	 *
	 * @return
	 */
	public Wall getWall(Compass facing){
		for(Wall wall : walls) {
			if(wall.getFacing().equals(facing)) {
				return wall;
			}
		}
		return null;
	}

	/**
	 * Returns the backdrop image that appears behind any wall images when drawn
	 * @return Returns the backdropImage.
	 */
	public BufferedImage getBackdropImage(){
		return backdropImage;
	}

	/**
	 * Returns SpaceFlags object
	 * @return Returns the flags.
	 */
	public SpaceFlags getFlags(){
		return flags;
	}

	/**
	 * Sets a SpaceFlags object
	 * @param flags The flags to set.
	 */
	public void setFlags(SpaceFlags flags){
		this.flags = flags;
	}

	/**
	 * Returns a GameEvent object.
	 * @return Returns the event.
	 */
	public GameEvent getEvent(){
		return event;
	}

	/**
	 * Sets a GameEvent object
	 * @param event The event to set.
	 */
	public void setEvent(GameEvent event){
		this.event = event;
	}

	/**
	 * Returns the backdrop image's name
	 * @return Returns the backdropImageName.
	 */
	public String getBackdropImageName(){
		return backdropImageName;
	}

	/**
	 * Sets a backdrop image's name.
	 * @param backdropImageName The backdropImageName to set.
	 */
	public void setBackdropImageName(String backdropImageName){
		this.backdropImageName = backdropImageName;
		backdropImage = FlatImageManager.getImage(backdropImageName);
	}
}
